Jak sprawdzić, czy tekst edittext jest adresem e-mail, czy nie?
Jak sprawdzić tekst edittext
jest adresem e-mail lub nie bez użycia javascript
i wyrażenia regularnego?
Tutaj użyłem {[2] } to działa, ale nie wyświetla się komunikat o błędzie.
18 answers
/**
* method is used for checking valid email id format.
*
* @param email
* @return boolean true for valid false for invalid
*/
public static boolean isEmailValid(String email) {
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
Podaj swój tekst edycji w tej funkcji .
Do weryfikacji poprawnej wiadomości e-mail potrzebujesz uwierzytelnienia po stronie serwera
Uwaga istnieje teraz wbudowana metoda w Androidzie, zobacz Odpowiedzi poniżej.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-15 07:29:19
Na Androidzie 2.2 + użyj tego:
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
Na przykład:
EditText emailid = (EditText) loginView.findViewById(R.id.login_email);
String getEmailId = emailid.getText().toString();
// Check if email id is valid or not
if (!isEmailValid(getEmailId)){
new CustomToast().Show_Toast(getActivity(), loginView,
"Your Email Id is Invalid.");
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-28 08:41:29
Wykonaj następujące kroki
Krok 1:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_below="@+id/textView_email"
android:layout_marginTop="40dp"
android:hint="Email Adderess"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/textView_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Email Validation Example" />
</RelativeLayout>
Krok 2:
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
Krok 3:
public class MainActivity extends Activity {
private EditText email;
private String valid_email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initilizeUI();
}
/**
* This method is used to initialize UI Components
*/
private void initilizeUI() {
// TODO Auto-generated method stub
email = (EditText) findViewById(R.id.editText_email);
email.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Is_Valid_Email(email); // pass your EditText Obj here.
}
public void Is_Valid_Email(EditText edt) {
if (edt.getText().toString() == null) {
edt.setError("Invalid Email Address");
valid_email = null;
} else if (isEmailValid(edt.getText().toString()) == false) {
edt.setError("Invalid Email Address");
valid_email = null;
} else {
valid_email = edt.getText().toString();
}
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email)
.matches();
} // end of TextWatcher (email)
});
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-27 15:18:36
Napisałem bibliotekę rozszerzającą EditText, która natywnie obsługuje niektóre metody walidacji i jest bardzo elastyczna.
Aktualne, jak piszę, natywnie obsługiwane (przez atrybuty xml) Metody walidacji to:
- regexp: dla niestandardowych regexp
- numeric : tylko dla pola numerycznego
- alpha : dla pola tylko Alfa
- / Align = "left" /
- email : sprawdza, czy pole jest poprawnym adresem e-mail
- creditCard : sprawdza, czy pole zawiera ważną kartę kredytową za pomocą algorytmu Luhna
- phone : sprawdza, czy pole zawiera prawidłowy numer telefonu
- domainName : sprawdza, czy pole zawiera poprawną nazwę domeny (zawsze przechodzi test na poziomie API
- ipAddress : sprawdza, czy pole zawiera poprawny adres ip webUrl: sprawdza, czy pole zawiera poprawny adres url (zawsze przechodzi test na poziomie API
- nocheck : niczego nie sprawdza. (Domyślnie)
Mam nadzieję, że ci się spodoba:)
Na podlinkowanej stronie znajdziesz również przykład walidacji poczty e-mail. Skopiuję relatywny fragment tutaj:
<com.andreabaccega.widget.FormEditText
style="@android:style/Widget.EditText"
whatever:test="email"
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
/>
Istnieje również aplikacja testowa prezentująca możliwości biblioteki.
To jest zrzut ekranu z aplikacja sprawdzająca poprawność pola e-mail.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-27 09:39:37
Jak wspomniano w jednej z odpowiedzi możesz użyć klasy Patterns
Jak poniżej:
public final static boolean isValidEmail(CharSequence target) {
if (target == null)
return false;
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
Przypadkiem, jeśli obsługujesz poziom API niższy niż 8, możesz po prostu skopiować plik Patterns.java
do swojego projektu i odwołać się do niego. Kod źródłowy Patterns.java
można pobrać z tego linku
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-27 10:13:46
W Twoim przypadku możesz użyć android.util.Patterns package
.
EditText email = (EditText)findViewById(R.id.user_email);
if(Patterns.EMAIL_ADDRESS.matcher(email.getText().toString()).matches())
Toast.makeText(this, "Email is VALID.", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Email is INVALID.", Toast.LENGTH_SHORT).show();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-07-15 11:45:19
Poniższy kod powinien być dla Ciebie przydatny.
String email;
check.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
checkEmail(email);
if (checkMail) {
System.out.println("Valid mail Id");
}
}
});
}
}
public static boolean checkEmail(String email) {
Pattern EMAIL_ADDRESS_PATTERN = Pattern
.compile("[a-zA-Z0-9+._%-+]{1,256}" + "@"
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" + "(" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" + ")+");
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-29 07:11:11
Aby sprawdzić poprawność adresu e-mail, wypróbuj ten prosty i łatwy kod.
String email = inputEmail.getText().toString().trim();
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches())
{
inputEmail.setError("Enter Valid Email Address");
inputEmail.requestFocus();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-30 20:13:15
Tutaj email to Twój email-id.
public boolean validateEmail(String email) {
Pattern pattern;
Matcher matcher;
String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-28 06:50:11
Apache Commons Validator może być używany, jak wspomniano w innych odpowiedziach.
Krok: 1) Pobierz plik jar z tutaj
Krok: 2) dodaj go do libs projektu
Import:
import org.apache.commons.validator.routines.EmailValidator;
Kod:
String email = "[email protected]";
boolean valid = EmailValidator.getInstance().isValid(email);
Oraz zezwalanie na lokalne adresy::
boolean allowLocal = true;
boolean valid = EmailValidator.getInstance(allowLocal).isValid(email);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-11 12:56:54
Prosta metoda
private boolean isValidEmail(String email)
{
String emailRegex ="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
if(email.matches(emailRegex))
{
return true;
}
return false;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-10 10:12:31
I Hope this code is beneficial for you
public class Register extends Activity
{
EditText FirstName, PhoneNo, EmailId,weight;
Button Register;
private static final Pattern EMAIL_PATTERN = Pattern
.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
private static final Pattern USERFIRSTNAME_PATTERN = Pattern
.compile("[a-zA-Z0-9]{1,250}");
private static final Pattern PHONE_PATTERN = Pattern
.compile("[a-zA-Z0-9]{1,250}");
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
Register=(Button) findViewById(R.id.register);
FirstName=(EditText)findViewById(R.id.person_firstname);
PhoneNo =(EditText)findViewById(R.id.phone_no);
EmailId=(EditText)findViewById(R.id.email_id);
weight=(EditText) findViewById(R.id.weight);
Register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sFirstName= FirstName.getText().toString();
sPhoneNo= PhoneNo.getText().toString();
sEmailId= EmailId.getText().toString();
sweight= weight.getText().toString();
if(sFirstName.equals("")||sPhoneNo.equals("")||sEmailId.equals("")||sweight.equals(""))
{
if ((!CheckUsername(sFirstName)))
{
Toast.makeText(Register.this, "FirstName can not be null",Toast.LENGTH_LONG).show();
}
else if ((!Checkphoneno(sPhoneNo)))
{
Toast.makeText(Register.this, "ENTER VALID mobile no ",Toast.LENGTH_LONG).show();
}
else if ((!CheckEmail(sEmailId)))
{
Toast.makeText(Register.this, "ENTER VALID EMAIL ID",Toast.LENGTH_LONG).show();
}
else if ((!Checkweight(sweight)))
{
Toast.makeText(Register.this, "ENTER Weight in kg",Toast.LENGTH_LONG).show();
}
}
}
private boolean CheckEmail(String sEmailId) {
return EMAIL_PATTERN.matcher(sEmailId).matches();
}
private boolean CheckUsername(String sFirstName) {
return USERFIRSTNAME_PATTERN.matcher(sFirstName).matches();
}
private boolean Checkphoneno(String sPhoneNo) {
return PHONE_PATTERN.matcher(sPhoneNo).matches();
}
private boolean Checkweight(String sweight) {
return Weight_PATTERN.matcher(sweight).matches();
}
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-25 12:10:24
public static boolean isEmailValid(String email) {
boolean isValid = false;
String expression = "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
// "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (!matcher.matches()) {
isValid = true;
}
return isValid;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-11-06 13:27:00
Dla walidacji e-mail spróbuj tego.
public boolean checkemail(String email)
{
Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-25 05:10:26
Możesz to sprawdzić za pomocą wyrażenia regularnego
public boolean isValid(String strEmail)
{
pattern = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
matcher = pattern.matcher(strEmail);
if (strEmail.isEmpty()) {
return false;
} else if (!matcher.matches()) {
return false;
}
else
{
return true;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-30 08:28:17
private boolean isValidEmailID(String email) {
String PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-23 06:51:33
Z Androidem.util.Wzory i Kotlin to bardzo proste. Jedna funkcja liniowa zwracająca wartość logiczną.
fun validateEmail(email: String) = Patterns.EMAIL_ADDRESS.matcher(email)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-26 19:15:02
W Kotlinie, adres e-mail można zweryfikować prostą metodą bez pisania dużo kodu i zawracać sobie głowę wyrażeniem regularnym, takim jak "^[_A-Za-z0-9 -\+]....".
Zobacz jak to jest proste:
fun validateEmail(emailForValidation: String): Boolean{
return Patterns.EMAIL_ADDRESS.matcher(emailForValidation).matches()
}
Po napisaniu tej metody walidacji e-mail musisz tylko wprowadzić swój e-mail, który chcesz zweryfikować. Jeżeli metoda validateEmail() zwraca true e-mail jest prawidłowy a jeżeli false to e-mail nie jest prawidłowy.
Oto przykład jak możesz użyć tej metody:
val eMail: String = emailEditText.text.toString().trim()
if (!validateEmail(eMail)){ //IF NOT TRUE
Toast.makeText(context, "Please enter valid E-mail address", Toast.LENGTH_LONG).show()
return //RETURNS BACK TO IF STATEMENT
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-11-12 17:19:48